home *** CD-ROM | disk | FTP | other *** search
- /*
- File: "MPPeriodicalTest.c"
-
- Description:
- This is a application to measure the intertask signaling time.
-
- Version: v0.0
-
- File Ownership:
-
- DRI: George Warner
-
- Other Contact:
-
- Technology: MultiTasking/MultiProcessing
-
- Copyright: © Copyright 2000 Apple Computer, Inc. All rights reserved.
-
- Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc.
- ("Apple") in consideration of your agreement to the following terms, and your
- use, installation, modification or redistribution of this Apple software
- constitutes acceptance of these terms. If you do not agree with these terms,
- please do not use, install, modify or redistribute this Apple software.
-
- In consideration of your agreement to abide by the following terms, and subject
- to these terms, Apple grants you a personal, non-exclusive license, under Apple’s
- copyrights in this original Apple software (the "Apple Software"), to use,
- reproduce, modify and redistribute the Apple Software, with or without
- modifications, in source and/or binary forms; provided that if you redistribute
- the Apple Software in its entirety and without modifications, you must retain
- this notice and the following text and disclaimers in all such redistributions of
- the Apple Software. Neither the name, trademarks, service marks or logos of
- Apple Computer, Inc. may be used to endorse or promote products derived from the
- Apple Software without specific prior written permission from Apple. Except as
- expressly stated in this notice, no other rights or licenses, express or implied,
- are granted by Apple herein, including but not limited to any patent rights that
- may be infringed by your derivative works or by other works in which the Apple
- Software may be incorporated.
-
- The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO
- WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
- WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
- COMBINATION WITH YOUR PRODUCTS.
-
- IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
- CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
- GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
- OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
- (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
- ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
- Writers:
-
- gaw - George Warner
-
- Change History (most recent first):
-
- <1> 02/29/00 gaw Initial build
-
- Author Initials:
-
- gaw - George Warner
-
- */
- //**************************************************************************
- #pragma mark compiler directives
-
- //**************************************************************************
- #pragma mark #Includes
- #include <MacTypes.h>
- #include <math64.h>
- #include <Multiprocessing.h>
- //#include <DriverServices.h>
-
- #include <stdio.h> // for printf & fflush
- #include <SIOUX.h> // for SIOUXSettings stuff
- #include <SIOUXGlobals.h> // for SIOUXQuitting
-
- //**************************************************************************
- #pragma mark typedefs, structs, enums, defines, etc.
- enum {
- aQueue = 0,
- aSemaphore,
- anEvent
- }gReflectOP;
-
- //**************************************************************************
- #pragma mark static (local) globals
- static MPOpaqueID gWaiterID,gPostID;
-
- //**************************************************************************
- #pragma mark static (local) functions
- static OSStatus ReflectorTask(void*)
- {
- void *p1,*p2,*p3;
- MPEventFlags events;
-
- while (true)
- {
- switch (gReflectOP)
- {
- case aQueue:
- MPWaitOnQueue((MPQueueID) gWaiterID,&p1,&p2,&p3,kDurationForever);
- break;
- case aSemaphore:
- MPWaitOnSemaphore((MPSemaphoreID) gWaiterID,kDurationForever);
- break;
- case anEvent:
- MPWaitForEvent((MPEventID) gWaiterID,&events,kDurationForever);
- break;
- default:
- return -123;
- }
- switch (gReflectOP)
- {
- case aQueue:
- MPNotifyQueue((MPQueueID) gPostID,&p1,&p2,&p3);
- break;
- case aSemaphore:
- MPSignalSemaphore((MPSemaphoreID) gPostID);
- break;
- case anEvent:
- MPSetEvent((MPEventID) gPostID,0x01010101);
- break;
- default:
- return -123;
- }
- }
- return -123;
- }
-
- static float HowLong(AbsoluteTime pEndTime,AbsoluteTime pBgnTime)
- {
- AbsoluteTime absTime;
- Nanoseconds nanosec;
-
- absTime = SubAbsoluteFromAbsolute(pEndTime,pBgnTime);
- nanosec = AbsoluteToNanoseconds(absTime);
- return (float) (UnsignedWideToUInt64(nanosec) / 1000.0f);
- }
-
- void main (void)
- {
- OSStatus anErr;
- MPTaskID task;
- UInt32 i,count;
- void *p1,*p2,*p3;
- MPEventFlags events;
- AbsoluteTime nowTime,bgnTime;
- float uSec;
-
- // Set the SIOUX window defaults
- SIOUXSettings.autocloseonquit = false;
- SIOUXSettings.asktosaveonclose = false;
- SIOUXSettings.showstatusline = false;
- SIOUXSettings.columns = 132;
- SIOUXSettings.rows = 24;
- SIOUXSettings.fontsize = 10;
- GetFNum("\pMonaco",&SIOUXSettings.fontid);
- SIOUXSettings.standalone = true;
-
- // Make sure that the MP library is loaded
- if (!MPLibraryIsLoaded())
- {
- printf("The MP library did not load.\n");
- return;
- }
-
- // Find the overhead up UpTime.Perform a bunch
- // of calls to average out cache effects.
-
- printf("\n");
-
- bgnTime = UpTime();
- for (i= 0;i<16;i++)
- nowTime = UpTime();
-
- uSec = HowLong(nowTime,bgnTime) / 16.0f;
- printf("UpTime overhead:%.3f usec \n",uSec);
-
- // Time intertask communication.
- printf("\n Queues \n");
- gReflectOP = aQueue;
- MPCreateQueue((MPQueueID*) &gWaiterID);
- MPCreateQueue((MPQueueID*) &gPostID);
-
- bgnTime = UpTime();
- anErr = MPCreateTask(ReflectorTask,NULL,0,NULL,0,0,kNilOptions,&task);
- nowTime = UpTime();
-
- uSec = HowLong(nowTime,bgnTime);
- printf("MPCreateTask overhead:%.3f usec (may vary significantly)\n",uSec);
- if (noErr != anErr)
- {
- printf("Task not created!\n");
- return;
- }
-
- count = 100000;
- bgnTime = UpTime();
- for (i= 0;i<count;i++)
- {
- MPNotifyQueue((MPQueueID)gWaiterID,0,0,0);
- while (true)
- {
- anErr = MPWaitOnQueue((MPQueueID)gPostID,&p1,&p2,&p3,kDurationImmediate);
- if (kMPTimeoutErr != anErr)break;
- }
- }
- nowTime = UpTime();
-
- uSec = HowLong(nowTime,bgnTime) / ((float)count /2.0); //Two trips.
- printf("Intertask signalling using queues overhead:%.3f usec \n",uSec);
-
- MPTerminateTask(task,123);
-
- // Time intertask communication.
- printf("\n Semaphores \n");
- gReflectOP = aSemaphore;
- MPCreateSemaphore(1,0,(MPSemaphoreID*)&gWaiterID);
- MPCreateSemaphore(1,0,(MPSemaphoreID*)&gPostID);
-
- bgnTime = UpTime();
- anErr = MPCreateTask(ReflectorTask,NULL,0,NULL,0,0,kNilOptions,&task);
- nowTime = UpTime();
-
- uSec = HowLong(nowTime,bgnTime);
- printf("MPCreateTask overhead:%.3f usec (may vary significantly)\n",uSec);
- if (noErr != anErr)
- {
- printf("Task not created!\n");
- return;
- }
-
- count = 100000;
- bgnTime = UpTime();
- for (i= 0;i<count;i++)
- {
- MPSignalSemaphore((MPSemaphoreID)gWaiterID);
- while (true)
- {
- anErr = MPWaitOnSemaphore((MPSemaphoreID)gPostID,kDurationImmediate);
- if (kMPTimeoutErr != anErr) break;
- }
- }
- nowTime = UpTime();
-
- uSec = HowLong(nowTime,bgnTime);
- uSec /= ((float)count /2.0);//Two trips.
- printf("Intertask signalling using sempahores overhead:%.3f usec \n",uSec);
-
- MPTerminateTask(task,123);
-
- // Time intertask communication.
- printf("\n Event Groups \n");
- gReflectOP = anEvent;
- MPCreateEvent((MPEventID*)&gWaiterID);
- MPCreateEvent((MPEventID*)&gPostID);
-
- bgnTime = UpTime();
- anErr = MPCreateTask(ReflectorTask,NULL,0,NULL,0,0,kNilOptions,&task);
- nowTime = UpTime();
- uSec = HowLong(nowTime,bgnTime);
-
- printf("MPCreateTask overhead:%.3f usec (may vary significantly)\n",uSec);
- if (noErr != anErr)
- {
- printf("Task not created!\n");
- return;
- }
-
- count = 100000;
- bgnTime = UpTime();
- for (i= 0;i<count;i++)
- {
- MPSetEvent((MPEventID)gWaiterID,0x01);
- while (true)
- {
- anErr = MPWaitForEvent((MPEventID)gPostID,&events,kDurationImmediate);
- if (kMPTimeoutErr != anErr)break;
- }
- }
- nowTime = UpTime();
-
- uSec = HowLong(nowTime,bgnTime) / ((float)count /2.0); //Two trips.
- printf("Intertask signalling using events overhead:%.3f usec \n",uSec);
-
- printf("\nPress command-Q to quit.");
- }